package com.kangtushizhe.handler;

import com.alibaba.fastjson2.JSON;
import com.kangtushizhe.entities.CommonResult;
import com.kangtushizhe.utils.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AppAuthenticationFailHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        CommonResult result = new CommonResult<>(400, "用户名或密码错误");
        String responseJson = JSON.toJSONString(result);
        WebUtil.renderString(response,responseJson);
    }
}
